package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.utils.common.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;

    @Override
    @Transactional
    public ResponseResult upload(MultipartFile multipartFile) throws IOException {
        //1. 校验参数 如果参数不对则返回错误信息
        if(null == multipartFile || multipartFile.isEmpty()){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2. 上传到minio
        //2.1 解决文件重名的问题
        String fileName = UUID.randomUUID().toString();
        String originalFilename = multipartFile.getOriginalFilename();
        fileName = fileName + originalFilename.substring(originalFilename.lastIndexOf("."));
        String path = fileStorageService.uploadImgFile("",
                fileName,
                multipartFile.getInputStream());

        //3. 保存数据到数据库
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(WmThreadLocalUtil.getUser().getId());
        wmMaterial.setType((short)0);
        wmMaterial.setUrl(path);
        wmMaterial.setIsCollection((short)0);
        wmMaterial.setCreatedTime(new Date());
        save(wmMaterial);

        //4. 构建返回值并返回
        return ResponseResult.okResult(wmMaterial);
    }

    /**
     * 分页擦好像
     *
     * @param wmMaterialDto
     * @return
     */
    @Override
    public ResponseResult listWmMaterial(WmMaterialDto wmMaterialDto) {
        //1. 校验并修正参数
        if(null == wmMaterialDto){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        wmMaterialDto.checkParam(); //修正参数

        //3. 根据分页方法的提示设置当前页和每页显示记录数
        IPage<WmMaterial> page = new Page<>(wmMaterialDto.getPage(), wmMaterialDto.getSize());
        //4. 根据分页方法的提示设置查询的条件
        LambdaQueryWrapper<WmMaterial> queryWrapper = new LambdaQueryWrapper<>();
        if(null != wmMaterialDto.getIsCollection()){
            queryWrapper.eq(WmMaterial::getIsCollection, wmMaterialDto.getIsCollection());
        }

        //设置查询当前登录用户的素材数据
        queryWrapper.eq(WmMaterial::getUserId, WmThreadLocalUtil.getUser().getId());

        //设置排序规则
        queryWrapper.orderByDesc(WmMaterial::getCreatedTime);
        //2. 调用service的通用分页方法完成分页
        page(page, queryWrapper);

        //5. 封装参数并返回
        PageResponseResult pageResponseResult = new PageResponseResult();
        pageResponseResult.setCurrentPage(wmMaterialDto.getPage());
        pageResponseResult.setSize(wmMaterialDto.getSize());
        pageResponseResult.setTotal((int)page.getTotal());
        pageResponseResult.setData(page.getRecords());

        return pageResponseResult;
    }
}
